﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
namespace WebApplication1
{
    public partial class ChiTietSanPham : System.Web.UI.Page
    {
        BLLSanPham sanpham;
        public ChiTietSanPham()
        { }
      
        public ChiTietSanPham(BLLSanPham _sanpham):this()
        {
            sanpham = _sanpham;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                sanpham = new BLLSanPham(Request.QueryString["Id"], "", "", "", "", "", "", "", "", "");
                DataList1.DataSource = sanpham.ShowChitiet;
                DataList1.DataBind();
            }
        }

        //protected void ImageButton_datmua_Click(object sender, ImageClickEventArgs e)
        //{
        //    BLLGioHang gio = (BLLGioHang)Session["GioHang"];
        //}

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "datmua")
            {
                BLLGioHang gio = (BLLGioHang)Session["GioHang"];
                string id = e.CommandArgument.ToString();
                Label thongbao = (Label)e.Item.FindControl("Lbthongbao");
                Label ten =  (Label)e.Item.FindControl("LbTen");
                Label gia = (Label)e.Item.FindControl("LbGia");
                DropDownList sl = (DropDownList)e.Item.FindControl("ddlsoluong");

                if (!gio.Exits(id))
                {
                    gio.Add(id, ten.Text, int.Parse(sl.SelectedValue), long.Parse(gia.Text));
                    Session["sohang"] = (int)Session["sohang"] + 1;
                    Response.Redirect("ChiTietSanPham.aspx?Id="+id+"");
                }
                else
                {
                    if (gio.CheckSoluong(id)+Convert.ToInt32(sl.SelectedValue) > 3)
                    {
                        thongbao.Text = "Số lượng mua vượt quá 3 ";
                    }
                    else
                    {
                        gio.Add(id, ten.Text, int.Parse(sl.SelectedValue), long.Parse(gia.Text));
                        Response.Redirect("ChiTietSanPham.aspx?Id=" + id + "");
                       // thongbao.Text = gio.CheckSoluong(id).ToString();
                    }
                }
              
            }
        }



    }
}
